<?php
/**
 * 独尊探霸服务
 * User: 谢宝新
 * Date: 2018/3/1
 * Time: 9:21
 */
ini_set('default_socket_timeout', -1);
set_time_limit(0);

define('PRERDS', 'DZTB');
define('DEBUG', true);
define('ROOT', __DIR__);
if (defined('DEBUG') && DEBUG == true) {
    error_reporting(E_ALL); //调试模式
} else {
    error_reporting(0);
}


if (PHP_SAPI != 'cli') {
    echo 'Script should be used under CLI'.PHP_EOL;
    exit;
}
if(!extension_loaded('swoole') || !extension_loaded('redis')){
    echo 'swoole and redis extensions is needed'.PHP_EOL;
    exit;
}

require_once ROOT . '/vendor/autoload.php';

$config = require_once ROOT . '/config/config.php';


//根据不同参数取得生成不同handler
$argv = getopt('s:h:p:u:', array("oh:", "op:"));

if (!isset($argv['s']) || !in_array($argv['s'], array(
        'data',
        'baseserver',
        'websocket',
        'hrwebsocket',
        'cmd',
        'config',
        'update',
        'config_lv',
        'halert',
        'http',
        'hrhttp',
        'httprest'
    ))) {
    echo "Invalid service type\n";
    return;
}

global $handler;
//启动服务
//数据服务器
if ($argv['s'] == 'data') {
    $config=array_merge($config,require_once "config/data.config.php");

    $serv_config = config('server');
    if (!isset($argv['h'])) $argv['h'] = '0.0.0.0';
    if (!isset($argv['p'])) $argv['p'] = 9502;
    if (!isset($argv['oh'])) $argv['oh'] = '0.0.0.0';
    if (!isset($argv['op'])) $argv['op'] = 9507;

    $handler = new \Handler\TanBa();

} elseif( $argv['s'] == 'config') {
    //配置服务器
    $config=array_merge($config,require_once "config/config.config.php");

    if (!isset($argv['h'])) $argv['h'] = '0.0.0.0';
    if (!isset($argv['p'])) $argv['p'] = 9503;
    $handler = new \Handler\Config();
}
elseif( $argv['s'] == 'config_lv') {
    // 配置服务器 启动 php index.php -s config_lv
    $config=array_merge($config,require_once "config/config.config.php");

    if (!isset($argv['h'])) $argv['h'] = '0.0.0.0';
    if (!isset($argv['p'])) $argv['p'] = 9505;
    $handler = new \Handler\Config_lv();
}

elseif( $argv['s'] == 'cmd') {
    //配置服务器
    $config=array_merge($config,require_once "config/cmd.config.php");

    if (!isset($argv['h'])) $argv['h'] = '0.0.0.0';
    if (!isset($argv['p'])) $argv['p'] = 9511;
    $handler = new \Handler\Commander();
}
elseif( $argv['s'] == 'update') {
    //配置服务器
    $config=array_merge($config,require_once "config/upgrade.config.php");

    if (!isset($argv['h'])) $argv['h'] = '0.0.0.0';
    if (!isset($argv['p'])) $argv['p'] = 9504;
    $handler = new \Handler\Upgrade();


}
elseif ($argv['s'] == 'websocket') {
    $config=array_merge($config,require_once "config/websocket.config.php");

    if (!isset($argv['h'])) $argv['h'] = '0.0.0.0';
    if (!isset($argv['p'])) $argv['p'] = 9501;
    $handler = new \Handler\WebSocket();

}
elseif ($argv['s'] == 'hrwebsocket') {
    $config=array_merge($config,require_once "config/hr.config.php");

    if (!isset($argv['h'])) $argv['h'] = '0.0.0.0';
    if (!isset($argv['p'])) $argv['p'] = 9512;
    $handler = new \Handler\HrWebSocket();
}
elseif($argv['s'] == 'hrhttp') {
    $config=array_merge($config,require_once "config/hr.config.php");

    if (!isset($argv['h'])) $argv['h'] = '0.0.0.0';
    if (!isset($argv['p'])) $argv['p'] = 9513;
    $handler = new \Handler\HrServerHttp();

}
elseif($argv['s'] == 'http') {
    $config=array_merge($config,require_once "config/http.config.php");

    if (!isset($argv['h'])) $argv['h'] = '0.0.0.0';
    if (!isset($argv['p'])) $argv['p'] = 9506;
    $handler = new \Handler\ServerHttp();

}
elseif($argv['s'] == 'httprest') {
    $config=array_merge($config,require_once "config/http.config.php");

    if (!isset($argv['h'])) $argv['h'] = '0.0.0.0';
    if (!isset($argv['p'])) $argv['p'] = 9514;
    $handler = new \Handler\ServerHttpRestful();

}
elseif ($argv['s'] == 'baseserver') {
	$config = array_merge($config, require_once "config/baseserver.config.php");
	$handler = new \Handler\BaseDataServer();
	(new \Core\BaseProcessPool($config['pool']['worker_num'], $config['pool']['ipc_type'], $config['pool']['msgqueue_key']))->run($handler);
	exit;
}

define('IP', $argv['h']);
define('PORT', $argv['p']);
//根据参数判断是否启动order服务
if (isset($argv['oh']) && isset($argv['op'])) {
    define('ORDER_IP', $argv['oh']);
    define('ORDER_PORT', $argv['op']);
}


//启动服务
if (in_array($argv['s'],['websocket','cmd','hrwebsocket','webscttest']) )
{
    (new \Core\WSServer($argv['h'], $argv['p'], SWOOLE_PROCESS, SWOOLE_SOCK_TCP))->run($handler, config('server'));
}elseif (in_array($argv['s'],['http','hrhttp','httprest']))
{
    (new \Core\HTTPServer($argv['h'], $argv['p']))->run($handler, config('server'));
    //http://103.251.36.122:9506/?type=898983
}
else {
    (new \Core\TBServer($argv['h'], $argv['p'], SWOOLE_PROCESS, SWOOLE_SOCK_TCP))->run($handler, config('server'));
}
//php index.php -s data --op 9707 --oh 0.0.0.0

